from openpyxl.chart import (
    PieChart,
    ProjectedPieChart,
    Reference
)
from openpyxl.chart.series import DataPoint
from openpyxl import Workbook

# 准备数据
data = [
    ['Pie', 'Sold'],
    ['Apple', 50],
    ['Cherry', 30],
    ['Pumpkin', 10],
    ['Chocolate', 40],
]

# 创建工作表
wb = Workbook()
ws = wb.active

# 向工作表中追加数据
for row in data:
    ws.append(row)

# 创建饼图
pie = PieChart()
# 饼图的标签
labels = Reference(ws, min_col=1, min_row=2, max_row=5)
# 饼图的数据
data = Reference(ws, min_col=2, min_row=1, max_row=5)
# 设置饼图的数据
pie.add_data(data, titles_from_data=True)
# 设置饼图的分类
pie.set_categories(labels)
# 设置饼图的标题
pie.title = "一张简单的饼图"

# Cut the first slice out of the pie
# 划分
slice = DataPoint(idx=0, explosion=20)
# 序列: 划分
pie.series[0].data_points = [slice]
# 添加饼图到工作表
ws.add_chart(pie, "D1")
# 设置饼图的标题
ws = wb.create_sheet(title="Projection")
wb.save("pie1.xlsx")
